package prettyprolog.engine.functions;

import prettyprolog.datatypes.Real;
import prettyprolog.datatypes.Term;
import prettyprolog.engine.Function;
import prettyprolog.engine.FunctionException;

public class Plus extends Function {

	private static final Plus inst = new Plus();
	
	public static Plus getInstance() {
		return inst;
	}
	
	@Override
	public Term apply(Term... args) throws FunctionException {
		double result = 0;
		for(Term t : args) {
			try {
				result += ((Real) t).getValue();
			} catch(ClassCastException e) {
				throw new FunctionException("Not a number: " + t, e);
			}
		}
		return new Real(result);
	}

}
